//
//  visitorView.swift
//  WKweiBo
//
//  Created by 阿拉斯加的狗 on 15/12/6.
//  Copyright © 2015年 阿拉斯加的🐶. All rights reserved.
//

import UIKit
import SnapKit
//设置代理
//protocol visitorViewDelegate: NSObjectProtocol {
//
//    //注册
//    func visitorViewDidRegister()
//
//    //登陆
//    func visitorViewDidLogin()
//}

class visitorView: UIView {
    
    //设置代理
//    weak var delegate: visitorViewDelegate?
    
    //监听代理方法
//    @objc private func clickLogin(){
//    
//        delegate?.visitorViewDidLogin()
//        
//        print("登陆")
//    }
//    
//    @objc private func clickRegister(){
//    
//        delegate?.visitorViewDidRegister()
//        
//        print("注册")
//    }
    
    //设置不同的界面
     func setupInfo(image: String?,title: String){
    
        messageLabel.text = title
    
        guard let imagename = image else{
        
            //开启动画
            startAnim()
            
            return
        
        }
        //设置图片显示
        iconView.image = UIImage(named: imagename)
        
        //设置小房子隐藏
        homeIconView.hidden = true
        
        //将遮盖移到最下面
        sendSubviewToBack(maskIconView)
        
    }
    
  //设置动画效果
    private func startAnim(){
    
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        
        anim.repeatCount = MAXFLOAT
        
        anim.duration = 20
        
        anim.removedOnCompletion = false
        
        //添加到图层
        iconView.layer.addAnimation(anim, forKey: nil)
        
        
    
    }
    
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        
        //如果写入这句 用storyBord的时候就会崩溃
//        fatalError("init(coder:) has not been implemented")
        
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    //懒加载空间
    
        //创建图标
    private lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")

    
        //设置遮盖图片
    private lazy var maskIconView :UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
    
        //创建房子
    private lazy var homeIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")

    
        //添加标签
    private lazy var messageLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
    
    
    
        //设置注册按钮
     lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), imageName: "common_button_white_disable")
    
    
    
        //设置登陆按钮
     lazy var loginButton: UIButton = UIButton(title: "登陆", color: UIColor.darkGrayColor(), imageName: "common_button_white_disable")
    
    
    
}


extension visitorView{

    //设置界面
    private func setupUI(){
    
        //添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)

    
        //取消控件的属性设置默认值
//        for v in subviews {
//            
//            v.translatesAutoresizingMaskIntoConstraints = false
//            
//        }
        
        
        //自动布局设置图片的位置
        
        //设置图标的位置
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        
        
        
        //设置房子的位置
//        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.centerY.equalTo(iconView.snp_centerY)
        }
    
        //设置标签的位置
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.width.equalTo(224)
            make.height.equalTo(36)
        }
        
        //设置注册的位置
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(36)
        }
        
        //设置登陆的位置
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
            loginButton.snp_makeConstraints { (make) -> Void in
                make.right.equalTo(messageLabel.snp_right)
                make.top.equalTo(messageLabel.snp_bottom).offset(16)
                make.width.equalTo(100)
                make.height.equalTo(36)
        }
        
        //设置蒙版遮盖
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask": maskIconView]))
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[regButton]", options: [], metrics: ["btnHeight": -36], views: ["mask": maskIconView, "regButton": registerButton]))
            maskIconView.snp_makeConstraints { (make) -> Void in
                make.top.equalTo(self.snp_top)
                make.left.equalTo(self.snp_left)
                make.right.equalTo(self.snp_right)
                make.bottom.equalTo(loginButton.snp_top)
        }
        
        
        //设置背景图片
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
        //监听按钮点击事件
        
//        loginButton.addTarget(self, action: "clickLogin", forControlEvents: .TouchUpInside)
//        
//        registerButton.addTarget(self, action: "clickRegister", forControlEvents: .TouchUpInside)
        
    }

    
    
    
    
}



    
    
    



